﻿using System;
using System.Collections.Generic;
using System.Linq;
using AspNet.Identity.MongoDB;
using Microsoft.Owin;
using Owin;
using Hangfire;
using Hangfire.Mongo;
using MongoDB.Driver;
using SignatureWebApi.Models;

[assembly: OwinStartup(typeof(SignatureWebApi.Startup))]

namespace SignatureWebApi
{
    /// <summary>
    /// 一个静态类储存MongoDb的实例
    /// </summary>
    public static class MongoDbInstanceStore
    {
        /// <summary>
        /// 测试用
        /// </summary>
        public static SignatureMongoDbInstance Client1;
        /// <summary>
        /// DataStore数据库实例
        /// </summary>
        public static SignatureMongoDbInstance DataStoreInstance;
        /// <summary>
        /// 航班数据库实例
        /// </summary>
        public static SignatureMongoDbInstance FlightCollInstance;
    }
    /// <summary>
    /// 程序启动
    /// </summary>
    public partial class Startup
    {
        /// <summary>
        /// 启动配置
        /// </summary>
        /// <param name="app"></param>
        public void Configuration(IAppBuilder app)
        {
            ConfigureAuth(app);

            //开发调试用
            //MongoDbIdentityInstanceStore.IdentityConnection = new MongoDbIdentityInstance("localhost", 27017, "root", "81700214", "admin");
            //MongoDbInstanceStore.DataStoreInstance = new SignatureMongoDbInstance("localhost", 27017, "uese_dataStore", "81700214", "DataStore");
            //MongoDbInstanceStore.FlightCollInstance = new SignatureMongoDbInstance("172.18.0.102", 27017, "monitorUser", "Moni658457!", "monitor");
            //GlobalConfiguration.Configuration.UseMongoStorage(new MongoClientSettings
            //{
            //    Credential = MongoCredential.CreateCredential("BackgroundProcess", "ProcessControl", "81700214"),
            //    Server = new MongoServerAddress("localhost", 27017)
            //}, "BackgroundProcess");
            //MongoDbInstanceStore.Client1 = new SignatureMongoDbInstance("localhost", 27017, "root", "81700214", "admin");
            //MongoDbIdentityInstanceStore.IdentityConnection = new MongoDbIdentityInstance("localhost", 27017, "monitorT2User", "T2dengjiqiao", "admin");
            //MongoDbInstanceStore.DataStoreInstance = new SignatureMongoDbInstance("localhost", 27017, "monitorT2User", "T2dengjiqiao", "admin");
            MongoDbIdentityInstanceStore.IdentityConnection = new MongoDbIdentityInstance(DbConnString.IdentityIpAdress, 27017, "Identity", "81700214", DbConnString.IdentityDatabase);
            MongoDbInstanceStore.DataStoreInstance = new SignatureMongoDbInstance(DbConnString.SignatureIpAdress, 27017, "UserDataStore", "81700214", DbConnString.SignatureDatabase);
            MongoDbInstanceStore.FlightCollInstance = new SignatureMongoDbInstance(DbConnString.FlightIpAdress, 27017, FlightDatabaseConnAcount.FlightDatabaseUser, FlightDatabaseConnAcount.FlightDatabasePassword, DbConnString.FlightDatabase);
            GlobalConfiguration.Configuration.UseMongoStorage(new MongoClientSettings
            {
                Credential = MongoCredential.CreateCredential(DbConnString.BackgroundProcessDatabase, "ProcessControl", "81700214"),
                Server = new MongoServerAddress(DbConnString.BackgroundProcessIpAdress, 27017)
            }, "BackgroundProcess");
            //RecurringTask.SyncForDataChangeDebug();
            app.UseHangfireServer();
            app.UseHangfireDashboard();
            //RecurringJob.RemoveIfExists("JOB1");
            //RecurringJob.AddOrUpdate("JOB1", () => RecurringTask.UpdateStaffMissionList(), Cron.Minutely);//自动生成工作任务
            //RecurringJob.RemoveIfExists("JOB2");
            //RecurringJob.AddOrUpdate("JOB2", () => RecurringTask.UpdateFlightMissionList(), Cron.MinuteInterval(4));//自动生成航班任务和签单任务再绑定
            //RecurringJob.RemoveIfExists("CleanWebSocketToken");
            //RecurringJob.AddOrUpdate("CleanWebSocketToken", () => RecurringTask.AutoCleanWebSocketToken(), Cron.Hourly);//清理数据库中过期的websocket存储
            //RecurringJob.RemoveIfExists("AutoPushMsg");
            //RecurringJob.AddOrUpdate("AutoPushMsg", () => RecurringTask.AutoPushMsg(), Cron.Hourly);//自动推送工作任务
            //RecurringTask.UpdateFlightMissionListTemp();
            var logger = NLog.LogManager.GetLogger("AppRunLog");
            logger.Info("WebApi启动");
            //RecurringTaskHelp.TestHandelFun();
            //var test=RecurringTaskHelp.GetEquDetailData(new[] {"1056", "1057","1058"}, "bridge", DateTime.Parse("2018-06-9 17:29:08"), DateTime.Parse("2018-06-14 18:01:11"));
            //RecurringTask.SyncForDataChangeDebug();
            //BackgroundJob.Enqueue(() =>  RecurringTask.SyncForDataChangeDebug());
            //var tttttttt=OtherHelper.CountUsingTime(DateTime.Now, DateTime.Now.AddHours(5).AddMinutes(5).AddSeconds(55));
            //RecurringTask.UpdateFlightMissionList();
            //RecurringJob.RemoveIfExists("some-id");
            //RecurringTask.UpdateStaffMissionList();
            //RecurringTask.UpdateFlightMissionList();
        }
    }
}
